package offer;

/**
 * <p>
 * 剑指 Offer 54. 二叉搜索树的第k大节点
 * </p>
 *
 * @author xiaozuolin
 * @date 2021/9/28 17:28
 */
public class Offer54 {
    int res;
    int k;

    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return res;
    }

    //中序遍历的倒序：右，根，左
    public void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.right);
        if (k == 0) {
            return;
        }
        if (--k == 0) {
            res = root.val;
        }
        dfs(root.left);
    }
}
